package com.zhengshang.web.controller.app;

import com.zhengshang.api.domain.AppVideo;
import com.zhengshang.api.service.IAppVideoService;
import com.zhengshang.common.annotation.Log;
import com.zhengshang.common.core.controller.BaseController;
import com.zhengshang.common.core.domain.AjaxResult;
import com.zhengshang.common.core.page.TableDataInfo;
import com.zhengshang.common.enums.BusinessType;
import com.zhengshang.common.utils.StringUtils;
import com.zhengshang.common.utils.poi.ExcelUtil;
import com.zhengshang.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 视频管理Controller
 * 
 * @author zhengshang
 * @date 2021-05-17
 */
@RestController
@RequestMapping("/app/education")
public class AppEducationController extends BaseController
{
    @Autowired
    private IAppVideoService appVideoService;
    @Autowired
    private ISysConfigService configService;

    /**
     * 查询视频管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:video:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppVideo appVideo)
    {
        startPage();
        appVideo.setVideoClass("2");
        List<AppVideo> list = appVideoService.selectAppVideoList(appVideo);
        return getDataTable(list);
    }

    /**
     * 导出视频管理列表
     */
    @PreAuthorize("@ss.hasPermi('app:video:export')")
    @Log(title = "视频管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(AppVideo appVideo)
    {
        List<AppVideo> list = appVideoService.selectAppVideoList(appVideo);
        ExcelUtil<AppVideo> util = new ExcelUtil<AppVideo>(AppVideo.class);
        return util.exportExcel(list, "video");
    }

    /**
     * 获取视频管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:video:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        AppVideo appVideo = appVideoService.selectAppVideoById(id);
        return AjaxResult.success(appVideo);
    }

    /**
     * 新增视频管理
     */
    @PreAuthorize("@ss.hasPermi('app:video:add')")
    @Log(title = "视频管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppVideo appVideo)
    {
        appVideo.setVideoClass("2");
        if (StringUtils.isNotBlank(appVideo.getVideoThumbnailImg())) {
            appVideo.setVideoThumbnailImg(appVideo.getVideoThumbnailImg().replaceAll(configService.selectConfigByKey("sys.oss.file.header"), ""));
        }
        return toAjax(appVideoService.insertAppVideo(appVideo));
    }

    /**
     * 修改视频管理
     */
    @PreAuthorize("@ss.hasPermi('app:video:edit')")
    @Log(title = "视频管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppVideo appVideo)
    {
        appVideo.setVideoClass("2");
        if (StringUtils.isNotBlank(appVideo.getVideoThumbnailImg())) {
            appVideo.setVideoThumbnailImg(appVideo.getVideoThumbnailImg().replaceAll(configService.selectConfigByKey("sys.oss.file.header"), ""));
        }
        return toAjax(appVideoService.updateAppVideo(appVideo));
    }

    /**
     * 删除视频管理
     */
    @PreAuthorize("@ss.hasPermi('app:video:remove')")
    @Log(title = "视频管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(appVideoService.deleteAppVideoByIds(ids));
    }
}
